﻿using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using System.Linq;
using Abp.Extensions;
using Camc.OutsourceProcession.ProduceTask.Dto;
using System;
using System.Collections.Generic;
using Abp.Collections.Extensions;
using System.Linq.Dynamic.Core;
using Abp.Linq.Extensions;
using Camc.OutsourceProcession.Customer;

namespace Camc.OutsourceProcession.ProduceTask
{
	public class OrderAppService: OutsourceProcessionAppServiceBase
	{
		private readonly IRepository<Order,long> _orderRepository;
		private readonly IRepository<Contact> _contactRepository;
		private readonly IRepository<SpecialTool,long> _specialToolRepository;
		private readonly IRepository<Process, long> _processRepository;

		public OrderAppService(IRepository<Order, long> orderRepository, IRepository<Contact> contactRepository, IRepository<SpecialTool, long> specialToolRepository, IRepository<Process, long> processRepository)
		{
			_orderRepository = orderRepository;
			_contactRepository = contactRepository;
			_specialToolRepository = specialToolRepository;
			_processRepository = processRepository;
		}

		public PagedResultDto<OrderMainOutput> GetOrders(GetOrdersInput input)
		{
			var query = _orderRepository.GetAllIncluding(c => c.Customer, d => d.ProductLogs).Where(c=>c.IsDeleted == false);

			query = query
				.WhereIf(!input.Customer.IsNullOrWhiteSpace(), c => c.Customer.CompanyName.Contains(input.Customer))
				.WhereIf(!input.Name.IsNullOrWhiteSpace(), c => c.Name.Contains(input.Name))
				.WhereIf(!input.PicNo.IsNullOrWhiteSpace(), c => c.PicNo.Contains(input.PicNo))
				.WhereIf(!input.TaskNo.IsNullOrWhiteSpace(), c => c.TaskNo.Contains(input.TaskNo))
				.WhereIf(!input.StartTime.ToString().IsNullOrWhiteSpace(), c => c.CreationTime >= input.StartTime)
				.WhereIf(!input.EndTime.ToString().IsNullOrWhiteSpace(), c => c.CreationTime < input.EndTime)
				.WhereIf(input.OnlyUrgency, c=>c.IsUrgency == true )
				;

			if (input.Sorting.IsNullOrWhiteSpace())
			{
				input.Sorting = "CreationTime desc";
			}

			var orderCount = query.Count();

			var orders = query
			   .OrderBy(input.Sorting)
			   .PageBy(input)
			   .ToList();

			var orderDtos = ObjectMapper.Map<List<OrderMainOutput>>(orders);			

			return new PagedResultDto<OrderMainOutput>(
				orderCount,
				orderDtos
				);
		}


		public CreateOrUpdateOrderDto GetOrderForEdit(NullableIdDto<long> input)
		{
			Order order = null;
			if (input.Id.HasValue)
			{
				order = _orderRepository.GetAllIncluding(c=>c.Customer).Where(c=>c.Id == input.Id.Value).FirstOrDefault();
			}

			var output = new CreateOrUpdateOrderDto();

			//order
			output = order != null
				? ObjectMapper.Map<CreateOrUpdateOrderDto>(order)
				: new CreateOrUpdateOrderDto();

			//Customer names
			output.Customers = _contactRepository
				.GetAll()
				.Select(c => new ComboboxItemDto(c.Id.ToString(), c.CompanyName ) { IsSelected = c.Id == output.CustomerId })
				.ToList();


			return output;
		}

		public void DeleteOrder(long orderId)
		{
			_orderRepository.Delete(orderId);
		}

		public long CreateOrUpdateOrder(CreateOrUpdateOrderDto input)
		{
			if (input.Id.HasValue)
			{
				var order = _orderRepository.Get(input.Id.Value);
				
				order.Name = input.Name;
				order.PicNo = input.PicNo;
				order.TaskNo = input.TaskNo;
				order.CustomerId = input.CustomerId;
				order.DeliveryTime = input.DeliveryTime;
				order.Size = input.Size;
				_orderRepository.Update(order);
				return order.Id;
			}
			else
			{
				var order = new Order();
				order.Name = input.Name;
				order.PicNo = input.PicNo;
				order.TaskNo = input.TaskNo;
				order.CustomerId = input.CustomerId;
				order.DeliveryTime = input.DeliveryTime;
				order.Size = input.Size;

				var orderId = _orderRepository.InsertAndGetId(order);

				//初始化工具与工序数据
				for(var i = 0; i < 5; i++)
				{
					var specialTool = new SpecialTool
					{
						OrderId = orderId
					};
					_specialToolRepository.Insert(specialTool);
				}

				for(var i = 0; i < 10; i++)
				{
					var process = new Process
					{
						OrderId = orderId
					};
					_processRepository.Insert(process);
				}

				return orderId;
			}

		}

		

	}
}
